#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "nrutil.h"


// Some useful generic functions -- Mark Jacobsen 2004

void matmult(double **a, int a1, int a2, double **b, int b1, int b2, double **c) {
	//matrix multiply a x b and return c, a is a1 x a2 and b is b1 x b2
	int i, j, k;
	for (i=1; i<=a1; i++) {
		for (j=1; j<=b2; j++) {
			c[i][j] = 0.0;
			for (k=1; k<=a2; k++) c[i][j] += a[i][k] * b[k][j];
		}
	}
}

void matcopy(double **a, double **b, int dim1, int dim2) {
	//copies a into b
	int i,j;
	for (i=1;i<=dim1;i++) for (j=1;j<=dim2;j++) b[i][j] = a[i][j];
}


void errorstop(char message[]) {
	printf("\n%s", message);
	printf("\n\nPROGRAM STOPPED");
	//scanf_s("\n");
}

void timestamp(char message[]) {
    time_t rawtime;
	
	time ( &rawtime );
	printf ( "\n%s: %s", message, asctime (localtime ( &rawtime )) );
	
}

void str2dvector(char inputstr[], double outputvector[], int n_vals) {
	char *tok;
	int i;
	tok = strtok(inputstr, ", \t"); //separator is "," or " "
	for(i=1;i<=n_vals;i++) {
		sscanf(tok, "%lf", &outputvector[i]);
		tok = strtok(NULL, ", ");
	}
}

void str2ivector(char inputstr[], int outputvector[], int n_vals) {
	char *tok;
	int i;
	tok = strtok(inputstr, ", \t"); //separator is "," or " "
	for(i=1;i<=n_vals;i++) {
		sscanf(tok, "%d", &outputvector[i]);
		tok = strtok(NULL, ", ");
	}
}

void trim(char s[]) {
	int n;
	for (n = strlen(s)-1; n >= 0; n--)
		if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') break;
	s[n+1] = '\0';
}
